package com.strongwell.awe.controller;

import com.strongwell.awe.annotation.SysLog;
import com.strongwell.awe.model.TbRoles;
import com.strongwell.awe.model.WxTag;
import com.strongwell.awe.model.WxUser;
import com.strongwell.awe.service.WxUserService;
import com.strongwell.awe.util.ResultUtil;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.httpclient.HttpException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/wx")
public class WxUserController {
    @Autowired
    private WxUserService wxUserServiceImpl;

    @RequestMapping("/userList")
    @RequiresPermissions("wx:user:list")
    public String WxUserList(){
        return "page/wx/userList";
    }

    @RequestMapping("getWxUserList")
    @RequiresPermissions("wx:user:list")
    @ResponseBody
    public ResultUtil getWxUserList(Integer page, Integer limit){
        return wxUserServiceImpl.selUsers(page,limit);
    }

    @RequestMapping("/editWxUser/{openid}")
    @RequiresPermissions("wx:user:update")
    public String editUser(@PathVariable("openid")String openid, Model model){
        System.out.println("修改个人信息页面！");
        WxUser wxUser = wxUserServiceImpl.selUserByOpenid(openid);
        System.out.println(wxUser.toString());
        model.addAttribute("user", wxUser);
        return "page/wx/editUser";
    }

    @RequestMapping("updateRemark")
    @RequiresPermissions("wx:user:update")
    @ResponseBody
    public ResultUtil editRemark(WxUser wxUser){
        try {
            wxUserServiceImpl.updateRemark(wxUser);
            return ResultUtil.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    @RequestMapping("/tagList")
    @RequiresPermissions("wx:tag:list")
    public String roleList() {
        return "page/wx/tagList";
    }

    @RequestMapping("/getTagList")
    @RequiresPermissions("wx:tag:list")
    @ResponseBody
    public ResultUtil getTagList(Integer page,Integer limit) {
        return wxUserServiceImpl.selTags(page, limit);
    }

    @RequestMapping("/addTag")
    @RequiresPermissions("wx:tag:add")
    public String addTag() {
        return "page/wx/addTag";
    }

    @SysLog(value="添加角色信息")
    @RequestMapping("/insTag")
    @RequiresPermissions("sys:role:save")
    @ResponseBody
    public ResultUtil insTag(WxTag tag) {
        int errcode = wxUserServiceImpl.addTag(tag.getName());
        if(errcode == -1){
            return new ResultUtil(500, "系统繁忙");
        }else if(errcode == 45157){
            return new ResultUtil(500, "标签名非法，请注意不能和其他标签重名");
        }else if(errcode == 45158){
            return new ResultUtil(500, "标签名长度超过30个字节");
        }else if(errcode == 45056){
            return new ResultUtil(500, "创建的标签数过多，请注意不能超过100个");
        }else if(errcode == -2){
            return new ResultUtil(500, "请求错误");
        }else {
            return ResultUtil.ok();
        }
    }

    @RequestMapping("/batchtagging")
    @RequiresPermissions("wx:tag:update")
    public String editRole(Integer tagId,HttpServletRequest req) {
        WxTag tag=new WxTag();
        tag.setId(tagId);
        req.setAttribute("tag", tag);
        return "page/wx/batchTagging";
    }

    @RequestMapping("batchUsers/{userStr}/{tagid}")
    @RequiresPermissions("wx:tag:update")
    @ResponseBody
    public ResultUtil batchUsers(@PathVariable("userStr")String userStr,@PathVariable("tagid")Integer tagid){
        int errcode =wxUserServiceImpl.batchUsers(userStr,tagid);
        if(errcode == -1){
            return new ResultUtil(500, "系统繁忙");
        }else if(errcode == 40032){
            return new ResultUtil(500, "每次传入的openid列表个数不能超过50个");
        }else if(errcode == 45159){
            return new ResultUtil(500, "非法的标签");
        }else if(errcode == 45059){
            return new ResultUtil(500, "有粉丝身上的标签数已经超过限制，即超过20个");
        }else if(errcode == 40003){
            return new ResultUtil(500, "传入非法的openid");
        }else if(errcode == 49003){
            return new ResultUtil(500, "传入的openid不属于此AppID");
        }else {
            return ResultUtil.ok();
        }
    }

    @RequestMapping("/getMaterialList")
    @RequiresPermissions("wx:material:list")
    @ResponseBody
    public String getMaterialList() {
        return "";
    }

    @RequestMapping("/materialList")
    @RequiresPermissions("wx:material:list")
    public String materiaList() {
        return "page/wx/materialList";
    }

    @RequestMapping("/addMaterial")
    @RequiresPermissions("wx:material:add")
    public String addMaterial() {
        return "page/wx/addMaterial";
    }

    @RequiresPermissions("wx:material:add")
    @ResponseBody
    @RequestMapping(value = "/uploadFilee", method =  {RequestMethod.POST,RequestMethod.GET})
    public ResultUtil uploadFilee(@RequestParam("file") MultipartFile file){
        System.out.println(2);

        File f = null;
        try {
            String fileName = file.getOriginalFilename();
            // 获取文件后缀
            String prefix=fileName.substring(fileName.lastIndexOf("."));
            f = new File(fileName);
            file.transferTo(f);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }

        String sss = wxUserServiceImpl.uploadFilee(f);
        System.out.println(ResultUtil.ok().toString());
        return ResultUtil.ok();
    }

    @RequestMapping("/getPicList")
    @RequiresPermissions("wx:material:list")
    @ResponseBody
    public ResultUtil imgList(@PathVariable("type")String type){
        System.out.println("获取图片数据");
        ResultUtil resultUtil =wxUserServiceImpl.selMaterials(type);
        return resultUtil;
    }
}
